//
//  PopView.m
//  IconMaster
//
//  Created by mac on 2017/7/31.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "PopView.h"

@interface PopView ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) UIImageView *contentImageView;
@property (nonatomic, strong) UITextField *nameTextFeild;
@property (nonatomic, strong) UIButton *textFeildButton;
@property (nonatomic, strong) UIButton *okButton;
@property (nonatomic, strong) NSString *urlSchemeStr;
@end
@implementation PopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame withImage:(UIImage *)image withURLScheme:(NSString *)urlSchemeStr{

    self = [super initWithFrame:frame];
    if (self) {
        self.image = image;
        self.urlSchemeStr = urlSchemeStr;
//        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//        [self addSubview:imageView];
        [self setUI];
    }
    
    return self;
}
- (void)setUI{

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelView)];
    [self addGestureRecognizer:tap];
    
//    UIImage *image_back = [UIImage imageNamed:@"popup"];
//    UIImageView *backImageView = [[UIImageView alloc] initWithImage:image_back];
//    [self addSubview:backImageView];
//    backImageView.sd_layout.centerXEqualToView(self).topSpaceToView(self, 100).widthIs(200).heightIs(216);
    UIView *backColorView = [[UIView alloc] initWithFrame:self.bounds];
    backColorView.backgroundColor = [UIColor blackColor];
    backColorView.alpha = 0.2;
    [self addSubview:backColorView];
    
    UIView *back_View = [[UIView alloc] init];
    back_View.backgroundColor = [UIColor whiteColor];
    [self addSubview:back_View];
    back_View.sd_layout.centerXEqualToView(self).topSpaceToView(self, 100).widthIs(250).heightIs(266);
    
    self.contentImageView = [[UIImageView alloc] initWithImage:self.image];
    [back_View addSubview:self.contentImageView];
    self.contentImageView.sd_layout.topSpaceToView(back_View, 27).centerXEqualToView(back_View).widthIs(100).heightIs(100);
    
    self.nameTextFeild = [[UITextField alloc] init];
    self.nameTextFeild.placeholder = @"编辑名字";
    self.nameTextFeild.delegate = self;
    self.nameTextFeild.layer.borderColor = Color(75, 215, 174).CGColor;
    self.nameTextFeild.layer.borderWidth = 1.0f;
    self.nameTextFeild.returnKeyType = UIReturnKeyDone;
    [back_View addSubview:self.nameTextFeild];
    self.nameTextFeild.sd_layout.leftSpaceToView(back_View, 30).rightSpaceToView(back_View, 30).topSpaceToView(self.contentImageView, 20).heightIs(30);
    
//    self.textFeildButton = [[UIButton alloc] init];
//    self.textFeildButton.layer.cornerRadius = 5.0;
//    self.textFeildButton.layer.masksToBounds = YES;
//    [self.textFeildButton addTarget:self action:@selector(editeButtonAction) forControlEvents:TouchUPInside];
//    [self.textFeildButton setTitle:@"编辑名字" forState:Normal];
//    self.textFeildButton.layer.borderColor = Color(75, 215, 174).CGColor;
//    self.textFeildButton.layer.borderWidth = 1.0f;
//    [back_View addSubview:self.textFeildButton];
//    self.textFeildButton.sd_layout.leftSpaceToView(back_View, 30).rightSpaceToView(back_View, 30).topSpaceToView(self.contentImageView, 20).heightIs(30);
    
    _okButton = [[UIButton alloc] init];
    [_okButton addTarget:self action:@selector(okButtonAction) forControlEvents:TouchUPInside];
    [_okButton setImage:[UIImage imageNamed:@"ok_app_edite"] forState:Normal];
    [back_View addSubview:_okButton];
    self.okButton.sd_layout.bottomSpaceToView(back_View, 5).leftSpaceToView(back_View, 2).rightSpaceToView(back_View, 2).heightIs(40);
}
- (void)editeButtonAction{
    [self.textFeildButton removeFromSuperview];
    [self.nameTextFeild becomeFirstResponder];
    
}
- (void)okButtonAction{
//    [self removeFromSuperview];
    if ([self.nameTextFeild.text length] !=0) {
        if ([self.delegate respondsToSelector:@selector(popViewButton:)]) {
            [self.delegate popViewButton:@{@"iconImage":self.image,
                                           @"title":self.nameTextFeild.text,
                                           @"value":self.urlSchemeStr}];
        }
    }else{
    
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"温馨提示","") message:NSLocalizedString(@"请填写名称","") delegate:self cancelButtonTitle:NSLocalizedString(@"确定","") otherButtonTitles:nil, nil];
        [alterView show];
    }
   
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)cancelView{
    [self removeFromSuperview];
//    [self.nameTextFeild resignFirstResponder];
}
@end
